from bokeh.palettes import Spectral5
from bokeh.sampledata.autompg import autompg as df
from bokeh.transform import factor_cmap
from bokeh.sampledata.autompg import autompg_clean as df
from bokeh.plotting import figure,show


def mpg():
    df.cyl = df.cyl.astype(str)
    #df.cyl = df.cyl.astype(str)
    group =df.groupby(['cyl','mfr'])#复合条件分组，【缸数，厂家】
    index_cmap = factor_cmap('cyl_mfr',palette=Spectral5, factors=sorted(df.cyl.unique()), end=1)
    # 画布
    p = figure(plot_width=800, plot_height=350, title="MEAN mpg by #cylinders and manu facturer",
               x_range=group, tooltips=[("MPG","@mpg_mean"),('cyl,Mfr',"cyl_mfr")]
              )
    # 柱状图
    p.vbar(x='cyl_mfr', top='mpg_mean', width=1, source=group,
          line_color="white",fill_color=index_cmap,)
    # 其他
    p.y_range.start = 0
    p.x_range.range_padding = 0.05
    p.xgrid.grid_line_color = None
    # p.xaxis.major_label = "Manufacturer grouped by # Cylinders"
    p.xaxis.major_label_orientation = 1.2 #x轴标签旋钮
    p.xgrid.grid_line_color = None
    
    return p